package com.ytjj.qmyx.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.dao.IconDao;
import com.ytjj.qmyx.admin.model.Icon;
import com.ytjj.qmyx.admin.model.request.IconRequest;
import com.ytjj.qmyx.admin.model.response.IconResponse;
import com.ytjj.qmyx.admin.service.IconService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class IconServiceImpl implements IconService {

    @Resource
    private IconDao iconDao;

    /**
     * 创建图标资源
     * @param icon
     * @return
     */
    @Transactional
    @Override
    public CommonResult create(Icon icon) {
        if(null == icon ){
            return CommonResult.failed("图标资源为空");
        }
        if(null == icon.getTitle() || "" == icon.getTitle()){
            return CommonResult.failed("图标标题不能为空");
        }
        if (null == icon.getType() || "" == icon.getType()){
            return CommonResult.failed("图标位置(类型)不能为空");
        }
        if(null == icon.getImgUrl() || "" == icon.getImgUrl()){
            return CommonResult.failed("图标链接不能为空");
        }
        if(null == icon.getRemarks()){
            icon.setRemarks("");
        }
        if(null == icon.getaPath()){
            icon.setaPath("");
        }
        if(null == icon.getSort()){
            icon.setSort(0);
        }
        if(null == icon.getStatus()){
            icon.setStatus(0);
        }
        icon.setCreateTime(new Date());
        icon.setUpdateTime(new Date());
        int count = iconDao.create(icon);
        if(count > 0){
            return CommonResult.success("创建图标资源成功");
        }
        return CommonResult.failed("创建图标资源失败");
    }

    @Override
    @Transactional
    public CommonResult delete(Integer id) {
        if(null == id){
            return CommonResult.failed("图标资源id为空,请选择正确的图标资源id");
        }
        Icon icon = iconDao.selectById(id);
        if(null == icon){
            return CommonResult.failed("不存在该图标资源");
        }
        int count = iconDao.delete(id);

        if(count < 0){
            return CommonResult.failed("删除图标资源失败");
        }
        return CommonResult.success("删除图标资源成功");
    }

    @Override
    @Transactional
    public CommonResult update(Icon icon) {
        Icon temp = iconDao.selectById(icon.getId());
        if(null == temp){
            return CommonResult.failed("没有找到修改对象,请选择正确的图标资源进行修改");
        }
        if(StrUtil.hasEmpty(icon.getTitle()) || StrUtil.isEmpty(icon.getTitle())){
            return CommonResult.failed("图标名称不能为空");
        }
        if(StrUtil.hasEmpty(icon.getType()) || StrUtil.isEmpty(icon.getType())){
            return CommonResult.failed("图标位置(类型)不能为空");
        }
        if(StrUtil.hasEmpty(icon.getImgUrl()) || StrUtil.isEmpty(icon.getImgUrl())){
            return CommonResult.failed("图标地址不能为空");
        }
        //创建时间不允许修改
        icon.setCreateTime(temp.getCreateTime());
        int count = iconDao.update(icon);
        if(count > 0){
            return CommonResult.success("修改图标资源成功");
        }
        return CommonResult.failed("修改图标资源失败");
    }

    @Override
    public CommonPage<IconResponse> selectList(IconRequest iconRequest) {
        PageHelper.startPage(iconRequest.getPageNum(), iconRequest.getPageSize());
        List<IconResponse> page = iconDao.selectList(iconRequest);
        return CommonPage.restPage(page);
    }

    @Override
    @Transactional
    public CommonResult updateStatus(Integer id, Integer status) {
        if(id == null || null == status){
            return CommonResult.failed("传入的参数不对,请选择正确的参数");
        }
        Icon icon = iconDao.selectById(id);
        if(null == icon){
            return CommonResult.failed("没有该图标资源,请选择正确的图标资源");
        }
        if(status != 1 && status != 0){
            return CommonResult.failed("无效的状态,请选择正确的状态");
        }

        int count = iconDao.updateStatus(id, status);
        if(count > 0){
            return CommonResult.success("修改状态成功");
        }
        return CommonResult.failed("修改状态失败");
    }
}
